package com.jade.store.wechat.service.impl;

import com.jade.bss.base.customer.Customer;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.store.wechat.biz.business.menu.MenuCreateBusiness;
import com.jade.store.wechat.biz.business.menu.MenuGetBusiness;
import com.jade.store.wechat.biz.business.user.UserGetBusiness;
import com.jade.store.wechat.biz.business.user.UserInfoBatchetBusiness;
import com.jade.store.wechat.biz.business.user.UserInfoBusiness;
import com.jade.store.wechat.biz.protocol.menu.MenuButtons;
import com.jade.store.wechat.biz.protocol.menu.MenuCreateReq;
import com.jade.store.wechat.biz.protocol.menu.MenuGetReq;
import com.jade.store.wechat.biz.protocol.menu.MenuGetRes;
import com.jade.store.wechat.biz.protocol.user.*;
import com.jade.store.wechat.biz.protocol.user.userTool.UserBatchGetReqTool;
import com.jade.store.wechat.dao.WechatMenuDao;
import com.jade.store.wechat.dao.WechatUserDao;
import com.jade.store.wechat.entity.WechatMenu;
import com.jade.store.wechat.entity.WechatUser;
import com.jade.store.wechat.service.WechatMenuFacade;
import com.jade.store.wechat.service.WechatUserFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: JinYa 2009_jinya@163.com
 * Date: 2016-5-5
 * Time: 10:53
 * Version : 1.0
 * Remark:
 * History:
 */
@Service("WechatMenuFacade")
public class WechatMenuFacadeImpl extends BaseFacadeImpl implements WechatMenuFacade {
    @Inject
    @Named("WechatMenuDao")
    private WechatMenuDao wechatMenuDao;

    public void send(Customer customer, String appid) throws  Exception{
        Criteria criteria=new Criteria();
        criteria.add(Restrictions.eq("ownerId", customer.getOwnerId()));
        criteria.addOrder(com.jade.framework.base.criterion.Order.desc("createTime"));
        PagingList<WechatMenu> list = wechatMenuDao.queryEntity(WechatMenu.class, criteria);
        if(list==null || list.size()<=0) return;
        MenuCreateBusiness business=new MenuCreateBusiness(appid);
        MenuCreateReq req=new MenuCreateReq();
        MenuButtons menuButtons=new MenuButtons();
        List<MenuCreateReq.Button> buttons=new ArrayList<MenuCreateReq.Button>();
        for(WechatMenu menu : list){
             if(menu.getParentId()==0){
                 MenuCreateReq.Button button=new MenuCreateReq.Button();
                 button.setName(menu.getName());
                 List<WechatMenu> sonMenus=this.getSonMenu(menu.getId());
                 if(sonMenus==null || sonMenus.size()<=0){
                     button.setType(menu.getType());
                     if(menu.getType().equals("view"))
                         button.setUrl(menu.getUrl());
                     else
                         button.setKey(menu.getViewkey());
                 }else{
                     List<MenuCreateReq.Button> subButtons=new ArrayList<MenuCreateReq.Button>();
                     for(WechatMenu sonmen : sonMenus){
                         MenuCreateReq.Button sonbutton=new MenuCreateReq.Button();
                         sonbutton.setName(sonmen.getName());
                         sonbutton.setType(sonmen.getType());
                         if(sonmen.getType().equals("view"))
                         sonbutton.setUrl(sonmen.getUrl());
                         else
                             sonbutton.setKey(sonmen.getViewkey());
                         subButtons.add(sonbutton);
                     }
                     button.setSub_button(subButtons);
                 }
                 buttons.add(button);
             }
        }
        MenuCreateReq.Button[] resultButton=new MenuCreateReq.Button[buttons.size()];
        if(resultButton.length>0){
            for(int i=0;i<resultButton.length;i++){
                resultButton[i]=buttons.get(i);
            }
            req.setButton(resultButton);
            business.run(req);
        }
    }
    public List<WechatMenu> getSonMenu(long parentId) throws  Exception{
        Criteria criteria=new Criteria();
        criteria.add(Restrictions.eq("parentId", parentId));
        PagingList<WechatMenu> list = wechatMenuDao.queryEntity(WechatMenu.class, criteria);
        return list;
    }
    /**
     * 同步微信用户
     * @param appid
     * @param appid
     * @throws Exception
     */
    public List<WechatMenu> synchronize(Customer customer, String appid)  throws  Exception{
        List<WechatMenu>  wechatUser=new ArrayList<WechatMenu>();
        MenuGetBusiness _business=new MenuGetBusiness(appid);
        MenuGetReq req=new MenuGetReq();
        MenuGetRes res=_business.run(req);
        return wechatUser;
    }
}
